<?php

namespace App\Console\Commands;

use App\Api\Models\StorageManageModel;
use App\V3\Api\Models\StorageShelfLocationModel;
use App\V3\Api\Models\StorageShelfModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class updateShelf extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shelf:updateStorage';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新相关表数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        set_time_limit(0);

        self::updateStorageManage();
    }

    static public function updateStorageManage()
    {
        $storageManageInfo = StorageManageModel::select(
            'a.id',
            'b.id as shelf_id',
            'c.id as location_id'
        )
            ->from('storage_manage as a')
            ->leftJoin('storage_shelf as b', 'b.storage_id', '=', 'a.storage_id')
            ->leftJoin('storage_shelf_location as c', 'c.shelf_id', '=', 'b.id')
            ->where([
//                'a.delete_flag' => 0,
                'a.shelf_id' => 0,
                'a.location_id' => 0,
                'location_type' => 1,
                'shelf_type' => 1,
            ])
            ->get()->toArray();

        try {
            batch()->update(new StorageManageModel, $storageManageInfo, 'id');

            DB::commit();
            return true;
        }
        catch(\Exception $e) {
            DB::rollBack();
            return $e->getMessage();
        }
    }
}
